package com.guava.string;

import com.google.common.base.Joiner;

import java.util.*;

/**
 * JoinerTester--GuavaTest
 *
 * @author: Jovi
 * @createTime: 2018-05-08 17:09
 **/
public class JoinerTester {

    public static void main(String[] args) {

        Joiner joiner1 = Joiner.on(",").skipNulls();

        joiner1.skipNulls(); // does nothing!分开写跳过null就不起作用了，因为实例不可改变

        System.out.println(joiner1.join("wrong", null, "wrong"));

        System.out.println("===============");

        List<String> list = new ArrayList<>();
        list.add("xiaoming");
        list.add("xiaohong");
        list.add("wangsan");

        List<String> list2 = new ArrayList<>();
        list2.add("xiaoming");
        list2.add(null);
        list2.add("wangsan");


        Joiner joiner = Joiner.on(",");
        String join1 = joiner.join(list);
        System.out.println("将list列表元素中的每个元素用指定符号连接：" + join1);

        String join2 = joiner.join(new StringBuilder("Joinner.join:"), list);
        System.out.println("拼接StringBuilder，将list列表元素中的每个元素用指定符号连接：" + join2);

        StringBuilder sb1 = joiner.appendTo(new StringBuilder("Joinner.appendTo:"), list);
        System.out.println("StringBuilder append ，将list列表元素中的每个元素用指定符号连接：" + sb1);

        StringBuilder sb2 = Joiner.on(",").skipNulls().appendTo(new StringBuilder("skipNulls1+String:"), "Hello", null, "adsfs");//rest不能为1个null;
        System.out.println(sb2);

        StringBuilder sb3 = Joiner.on(",").skipNulls().appendTo(new StringBuilder("skipNulls2+list:"), list2);
        System.out.println(sb3);

        StringBuilder sb4 = Joiner.on(",").useForNull("NULL").appendTo(new StringBuilder("useForNull+list:"), list2);
        System.out.println(sb4);

        StringBuilder sb5 = Joiner.on(",").useForNull("NULL").appendTo(new StringBuilder("useForNull+list:"), list2);
        System.out.println(sb5);

        String join3 = Joiner.on(",").useForNull("none").join(new StringBuilder("Joinner.join.useForNull:"),1, null,null);
        System.out.println(join3);

        String join4 = Joiner.on(",").skipNulls().join(Arrays.asList(1, 2, 3, 4, null, 6));
        System.out.println(join4);

        Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        System.out.println(Joiner.on(",").withKeyValueSeparator("=").join(map));

    }
}
